package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.HttpClient;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.ApplicationModuleBiz;
import com.useeinfo.oa.modules.base.biz.EnterpriseBiz;
import com.useeinfo.oa.modules.base.biz.PersonnnelOrganizationBiz;
import com.useeinfo.oa.modules.base.biz.SystemUserOrganizationBiz;
import com.useeinfo.oa.modules.base.entity.ApplicationModule;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Organization;
import com.useeinfo.oa.modules.base.entity.SystemUserOrganization;
import com.useeinfo.oa.modules.security.biz.OrganizationBiz;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/allCharts/dashBoardCharts")
public class DashboardChartsAPI {
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private ApplicationModuleBiz applicationModuleBiz;
    @Autowired
    private SystemUserOrganizationBiz systemUserOrganizationBiz;
    @Autowired
    private OrganizationBiz organizationBiz;
    @Autowired
    private PersonnnelOrganizationBiz personnnelOrganizationBiz;

    /**
     * 部门人员接单数
     */
    @RequestMapping("/getOrganizationReceiveNum")
    @ResponseBody
    public ResultDto getOrganizationReceiveNum(String type,String organizationId,String enterpriseId) {//一个人员可能属于多个部门，报表顶部根据部门切换选项卡传入部门id
        //type 1周报，2月报，3季度报
        Organization organization = organizationBiz.findModel(StringConverters.null2Long(organizationId));
        String organizationName = organization.getOrganizationName();

        String start;
        String end;
        if (type.equals("week")) {//周报
            start = DateTimeUtils.formatDateToString(DateTimeUtils.getFirstDateOfWeek(new Date()));//获取本周一的日期
            end = DateTimeUtils.formatDateToString(DateTimeUtils.getLastDateOfWeek(new Date()));//本周日的日期
        } else if (type.equals("month")) {//月报
            start = DateTimeUtils.formatDateToString(DateTimeUtils.getFirstDateOfMonth(new Date())); //获取本月初的日期
            end = DateTimeUtils.formatDateToString(DateTimeUtils.getLastDateOfMonth(new Date()));//本月末的日期
        } else {//季度报
            start = DateTimeUtils.formatDateToString(DateTimeUtils.getCurrentQuarterStartTime(new Date()));//获取当年的第一天
            end = DateTimeUtils.formatDateToString(DateTimeUtils.getCurrentQuarterEndTime(new Date()));//获取当年的最后一天
        }
        JSONObject jsonObject = new JSONObject();
        JSONArray dataTitle = new JSONArray();//模块名称
        JSONArray dataName = new JSONArray();//人员名称
        JSONArray data = new JSONArray();//人员接单数
        //获取该部门下的所有人员数据
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("organizationId", organizationId);
        queryParam.getSqlMap().put("enterpriseId", enterpriseId);
        queryParam.setSqlOrder("order by systemUser.id");
        List<SystemUserOrganization> systemUserOrganizations = systemUserOrganizationBiz.findList(queryParam);
        if (systemUserOrganizations.size() < 1) {
            // 暂无人员
            dataName.add("No personnel");
        } else {
            for (SystemUserOrganization x : systemUserOrganizations) {
                dataName.add(x.getSystemUser().getUserName());
            }
        }

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("organizationId",organizationId);
        dataMap.put("start",start);
        dataMap.put("end",end);
        QueryParam queryParam1 = new QueryParam();
        queryParam1.getSqlMap().put("enterpriseId", enterpriseId);
        List<ApplicationModule> applicationModules = applicationModuleBiz.findList(queryParam1);
        for (ApplicationModule x : applicationModules) {
            if ("MAINTAIN".equals(x.getRemark())) {        //维护保养
                dataTitle.add("PPM");
                JSONObject maintainData = new JSONObject();
                maintainData.put("name", "PPM");
                maintainData.put("stack", "数据报告");
                maintainData.put("type", "bar");
                maintainData.put("barWidth", "10");
                String ppmDataUrl = Configuration.getConfigurationByName("hostUrl") + "maintainCharts/getOrganizationReceiveNum.action";
                JSONArray ppm = JSONObject.parseArray(HttpClient.doPost(ppmDataUrl, dataMap));
                maintainData.put("data", ppm);
                data.add(maintainData);
            } else if ("INSPECTION".equals(x.getRemark())) { //抄表巡检
                dataTitle.add("Daily Check");
                JSONObject inspectionData = new JSONObject();
                inspectionData.put("name", "Daily Check");
                inspectionData.put("stack", "数据报告");
                inspectionData.put("type", "bar");
                inspectionData.put("barWidth", "10");
                String dailyCheckDataUrl = Configuration.getConfigurationByName("hostUrl") + "inspectionCharts/getOrganizationReceiveNum.action";
                JSONArray dailyCheck = JSONObject.parseArray(HttpClient.doPost(dailyCheckDataUrl, dataMap));
                inspectionData.put("data", dailyCheck);
                data.add(inspectionData);
            } else if ("REPAIR".equals(x.getRemark())) {        //维修管理
                dataTitle.add("M&R");
                JSONObject repairData = new JSONObject();
                repairData.put("name", "M&R");
                repairData.put("stack", "数据报告");
                repairData.put("type", "bar");
                repairData.put("barWidth", "10");
                String mrDataUrl = Configuration.getConfigurationByName("hostUrl") + "repairOrderCharts/getOrganizationReceiveNum.action";
                JSONArray mr = JSONObject.parseArray(HttpClient.doPost(mrDataUrl, dataMap));
                repairData.put("data", mr);
                data.add(repairData);
            }
            else if ("TASK".equals(x.getRemark())) {        //任务工单
                dataTitle.add("Others");
                JSONObject taskData = new JSONObject();
                taskData.put("name", "Others" +
                        "");
                taskData.put("stack", "数据报告");
                taskData.put("type", "bar");
                taskData.put("barWidth", "10");
                String taskDataUrl = Configuration.getConfigurationByName("hostUrl") + "taskOrderCharts/getOrganizationReceiveNum.action";
                JSONArray task = JSONObject.parseArray(HttpClient.doPost(taskDataUrl, dataMap));
                taskData.put("data", task);
                data.add(taskData);
            }
        }
        jsonObject.put("dataTitle", dataTitle);
        jsonObject.put("dataName", dataName);
        jsonObject.put("data", data);
        jsonObject.put("code","00000000");
        jsonObject.put("organizationName",organizationName);
        return ResultUtil.success(jsonObject);
    }


}
